package my.home;

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class gmail {

	public boolean sendEmailToInvitee(ArrayList<String> receiverEmailList, String pollId, String viewName) {
		final String username = "meeting0810@gmail.com";
		final String password = "ailab405";

		boolean result = true;

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});

		try {
			for (String receiverEmail : receiverEmailList) {
				Message message = new MimeMessage(session);
				message.setFrom(new InternetAddress("meeting0810@gmail.com"));
				message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverEmail));
				message.setSubject("Meeting Planner에서 누군가가 당신을 초대하였습니다.");
				message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!\n"
						+ "Link : http://203.237.183.109/project/data/" + viewName + ".jsp?id=" + pollId);

				Transport.send(message);
			}

			System.out.println("Done");

		} catch (MessagingException e) {
			result = false;
			throw new RuntimeException(e);
		}
		return result;
	}

	public boolean sendEmailToInitiater(String receiverEmail, String pollId, String viewName) {
		final String username = "meeting0810@gmail.com";
		final String password = "ailab405";

		boolean result = true;

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});

		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("meeting0810@gmail.com"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverEmail));
			message.setSubject("Meeting Planner에서 생성한 일정 정보입니다.");
			message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!\n"
					+ "Link : http://203.237.183.109/project/data/" + viewName + ".jsp?id=" + pollId);
			Transport.send(message);

			System.out.println("Done");

		} catch (MessagingException e) {
			result = false;
			throw new RuntimeException(e);
		}
		return result;
	}
}